package org.gruposp2p.dnie.client.ui.dialogs;

import java.util.List;
import org.gruposp2p.dnie.client.dto.DocumentToSignDTO;
import org.gruposp2p.dnie.client.dto.DocumentToSignOptionDTO;
import org.gruposp2p.dnie.client.ui.panel.MainPanel;
import org.gruposp2p.dnie.client.util.DateUtils;
import org.gruposp2p.dnie.client.util.URLConstants;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public class DocumentToSignDetailsDocumentPanel extends Composite {

    private TextBox titleTextBox;
    private TextArea contentTextArea;

    public DocumentToSignDetailsDocumentPanel() { }
    
    DocumentToSignDTO documentToSign;

    public DocumentToSignDetailsDocumentPanel(DocumentToSignDTO document) {
        this.documentToSign = document;
        FlexTable layout = new FlexTable();
        CellFormatter cellFormatter = layout.getCellFormatter();
        initWidget(layout);
        layout.clear();
        layout.setCellSpacing(6);
        int widgetRow = 0;
        layout.setHTML(widgetRow++, 0,
            MainPanel.constants.SignatureDataDialogTitleLabel());
        titleTextBox = new TextBox ();
        titleTextBox.setWidth("350");
        titleTextBox.setText(document.title);
        layout.setWidget(widgetRow++, 0, titleTextBox);
        layout.setWidget(widgetRow++, 0, 
        		URLConstants.getDocumentToSingHTMLUrl(document.id));        
        layout.setHTML(widgetRow++, 0,
        		MainPanel.constants.SignatureDataDialogContentLabel());
        contentTextArea = new TextArea();
        contentTextArea.setWidth("400");
        contentTextArea.setHeight("180");
        contentTextArea.setText(document.content);
        layout.setWidget(widgetRow++, 0, contentTextArea);
        layout.setWidget(widgetRow++, 0, getDatePanel(document));
        if(DocumentToSignDTO.DOCUMENT_TYPE_VOTE.equals(
        		document.type)) {
            layout.setWidget(
                    widgetRow++, 0, getVotingOptionsPanel(document.options));
        }
        titleTextBox.setReadOnly(true);
        contentTextArea.setReadOnly(true);
    }
    
    private HorizontalPanel getDatePanel (DocumentToSignDTO document) {
        HorizontalPanel datePanel = new HorizontalPanel();
        datePanel.setSpacing(15);
        VerticalPanel dateBeginPanel = new VerticalPanel();
        dateBeginPanel.add(new HTML(MainPanel.constants.dateBegin()));
        TextBox dateBeginBox;
        dateBeginBox = new TextBox ();
        dateBeginBox.setText(DateUtils.getStringFromDate(document.dateBegin));
        dateBeginBox.setWidth("150px");
        dateBeginPanel.add(dateBeginBox);
        datePanel.add(dateBeginPanel);
        VerticalPanel dateEndPanel = new VerticalPanel();
        dateEndPanel.add(new HTML(MainPanel.constants.dateEnd()));
        TextBox dateEndBox;
        dateEndBox = new TextBox ();
        dateEndBox.setText(DateUtils.getStringFromDate(document.dateEnd));
        dateEndBox.setWidth("150px");
        dateEndPanel.add(dateEndBox);
        datePanel.add(dateEndPanel);        
        return datePanel;
    }
    
    private ScrollPanel getVotingOptionsPanel (
    		List<DocumentToSignOptionDTO> options) {
        final VerticalPanel verticalPanel = new VerticalPanel();
        FlexTable layout = new FlexTable();
        layout.setCellSpacing(6);
        layout.clear();
        int row = 0;
        FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
        HTML header =
                new HTML(MainPanel.constants.votingOptionPanelCaption());
        layout.setWidget(row++, 0, header);
        int optionIndex = 1;
        for (DocumentToSignOptionDTO option : options) {
            final HorizontalPanel horizontalPanel = new HorizontalPanel();
            horizontalPanel.setSpacing(6);
            HTML optionIndexHTML =
                new HTML(String.valueOf(optionIndex++) + ". ");
            TextBox optionText = new TextBox();
            optionText.setReadOnly(true);
            optionText.setText(option.content);
            optionText.setWidth("240");
            horizontalPanel.add(optionIndexHTML);
            horizontalPanel.add(optionText);
            layout.setWidget(row++, 0, horizontalPanel);
        }
        verticalPanel.add(layout);
        verticalPanel.setWidth("100%");
        //verticalPanel.setStyleName("dnie-inner-panel");
        ScrollPanel scroller = new ScrollPanel(verticalPanel);
        scroller.setSize("400px", "150px");
        scroller.setStyleName("dnie-inner-panel");
        return scroller;
    }
    
}